package ar.edu.itba.pod.printer.impl;

import java.util.LinkedList;
import java.util.Queue;

import ar.edu.itba.pod.printer.Document;
import ar.edu.itba.pod.printer.Printer;
import ar.edu.itba.pod.printer.PrinterDispatcher;

public class SimpleAsyncPrinterDispatcher  implements PrinterDispatcher {

	private Printer printer= new Printer();
	private Queue<Document> queue= new LinkedList<Document>();
	private MyThread mythread;
	
	public SimpleAsyncPrinterDispatcher() {
		mythread= new MyThread(){
			public void excecute(){
				printIfNotEmpty();
			}
		};
		mythread.start();
	}
	
	@Override
	public void finishPrintRequests() {
		finishWorks();
		mythread.finish();
	}

	@Override
	public void printDocument(Document document) {
		queue.add(document);
	}
	public void finishWorks(){
		printIfNotEmpty();
	}
		
	public void printIfNotEmpty(){
		if( !queue.isEmpty()){
			printer.print(queue.peek());
		}	
	}

}
